﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

/*
 * DamageResult
 *
 *
 *
 * 依赖：
 * puppet
 *
 * ！不写重复代码！
 * @author TTun
 * @date 7/12/2017 5:16:56 PM
 */
namespace TTGameEngine {
public enum DamageActionID : byte {
    Normal = 0,//啥也没
    BreakOut, //暴击
    HideOut,//闪避
    CureOut,//治疗
    Defense,//被护盾抵挡
    DamageReturn,//反弹的伤害
    vampire,//吸血伤害
}

public enum DamageValueID : byte {
    DamageUnKown = 0,//未知类型 == DamageAllKown
    DamageMelee,//物攻伤害
    DamageMagic,//魔法伤害
    DamageAllKown,//任何伤害类型
}

//受伤数据作用结果
public struct objOnDamage {
    public bool isPlay;
    public bool isBeat;
    public bool isColor;

    public void Set(bool p, bool b, bool c) {
        isPlay = p;
        isBeat = b;
        isColor = c;
    }

    public void Clear() {
        isPlay = false;
        isBeat = false;
        isColor = false;
    }
}


public class DamageResult {
    public int damageConfigID;
    public DamageActionID actionID;
    public int damage;
    public DamageValueID damageValueID;
    public int[] buffs;
    public Puppet attacker;
    public int releaseID; //技能iD
    public string woundEffect;

    public DamageResult() {
        damageConfigID = -1;
        actionID = DamageActionID.Normal;
        damage = 0;
        damageValueID = DamageValueID.DamageUnKown;
        attacker = null;
        releaseID = -1;
    }

    public static DamageResult CreateDamageResult(Puppet hit, SkillReleaseData data) {
        if(hit == null || data == null) {
            return null;
        }
        DamageResult result = new DamageResult();

        result.damageConfigID = data.info.damageConfigID;
        result.damageValueID = data.info.damageValueID;
        float ratio = (data.info.damageRatio / 100f);
        float randomValue = UnityEngine.Random.Range(0.6f, 1.0f);

        if(data.IsCure) {
            //增益技能
            result.damage = (int)data.owner.AttackPoint;
        } else {
            result.damage = (int)((data.owner.AttackPoint - hit.DefensePoint) * randomValue);
            if(result.damage < 0) { result.damage = 1; }
        }
        result.damage = (int)(result.damage * ratio);
        result.damage += data.info.basicDamage;

        int breakRandom = UnityEngine.Random.Range(0, 100);
        int hideRandom = UnityEngine.Random.Range(0, 100);

        if(result.damage < 0) {
            result.actionID = DamageActionID.CureOut;
        } else if(hideRandom > 90) {
            result.damage = 0;
            result.actionID = DamageActionID.HideOut;
        } else if(breakRandom > 80) {
            result.damage *= 2;
            result.actionID = DamageActionID.BreakOut;
        } else {
            result.actionID = DamageActionID.Normal;
        }
        result.attacker = data.owner;
        result.releaseID = (int)data.info.skillID;
        result.woundEffect = data.info.woundEffect;

        List<int> tempBuff = new List<int>();
        if(data.info.buffId != null) {
            for(int i = 0; i < data.info.buffId.Length; i++) {
                int buffID = data.info.buffId[i];
                if(BuffReadInfo.Instance.IsAddBuff(buffID)) {
                    tempBuff.Add(buffID);
                }
            }
        }
        result.buffs = tempBuff.ToArray();

        return result;
    }


}

}